package com.woniuxy.creditsystem.systemmag.service.impl;

import com.github.pagehelper.PageInfo;
import com.woniuxy.creditsystem.common.bean.ResultMsg;
import com.woniuxy.creditsystem.common.bean.UserInfo;
import com.woniuxy.creditsystem.common.mapper.RoleMenuMapper;
import com.woniuxy.creditsystem.common.mapper.UserMapper;
import com.woniuxy.creditsystem.systemmag.service.IRoleMenuService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: hejie
 * @date: 2020/12/14 15:55
 * @description:
 */
@Transactional(readOnly = true)
@Service
public class RoleMenuServiceImpl implements IRoleMenuService {

    @Resource
    private RoleMenuMapper roleMenuMapper;

    /**
     * 修改角色和菜单的对应关系
     * @author hejie
     * @param roleId 被修改的角色的id
     * @param menuPermissionArray 被修改的角色的新权限
     * @return 受影响的行数
     */
    @Override
    @Transactional(isolation = Isolation.REPEATABLE_READ,rollbackFor = Exception.class)
    public int updateRoleMenuBean(Long roleId,String menuPermissionArray) {
        List<Long> menuIds=new ArrayList<>();
        if (menuPermissionArray.contains("1,3")){
            menuIds.add(3L);
        }
        if (menuPermissionArray.contains("1,4")){
            menuIds.add(4L);
        }
        if (menuPermissionArray.contains("1,5")){
            menuIds.add(5L);
        }

        List<Long> fullMenuIds=new ArrayList<>();
        fullMenuIds.add(3L);
        fullMenuIds.add(4L);
        fullMenuIds.add(5L);

        if (!fullMenuIds.equals(menuIds)){
            //删除
            for (int i=0; i<fullMenuIds.size(); i++){
                //根据id先删除（后面再添加）
                roleMenuMapper.deleteRoleMenu(roleId, fullMenuIds.get(i));
            }
            //添加
            for (int i=0; i<menuIds.size(); i++){
                //根据id先删除（后面再添加）
                roleMenuMapper.saveRoleMenuBean(roleId, menuIds.get(i));
            }
        }

        return 0;
    }

    @Transactional(isolation = Isolation.REPEATABLE_READ,rollbackFor = Exception.class)
    @Override
    public int saveRoleMenuBean(Long roleId, String menuPermissionArray) {
        List<Long> menuIds=new ArrayList<>();
        if (menuPermissionArray.contains("1,3")){
            menuIds.add(3L);
        }
        if (menuPermissionArray.contains("1,4")){
            menuIds.add(4L);
        }
        if (menuPermissionArray.contains("1,5")){
            menuIds.add(5L);
        }
        if (menuPermissionArray.contains("others")){
            for (Long i=6L; i<=53; i++){
                menuIds.add(i);
            }
        }
        //传入roleInfo.id  和menuIds  （要用list，在sql中）
        for (int i=0; i<menuIds.size()-1; i++){
            roleMenuMapper.saveRoleMenuBean(roleId, menuIds.get(i));
        }
        //把最后一个的插入，作为整体的结果：可能不科学
        return roleMenuMapper.saveRoleMenuBean(roleId, menuIds.get(menuIds.size()-1));
    }

   /* @Override
    public int deleteUserBean(Long id) {
        return 0;
    }

    @Override
    public UserInfo getOneById(Long id) {
        return null;
    }

    @Override
    public PageInfo<UserInfo> findAllByParams(int pageNum, int pageSize, UserInfo userInfo) {
        return null;
    }

    @Override
    public ResultMsg write2excel(List<UserInfo> tableDataJson, String path) throws IOException {
        return null;
    }*/
}
